#include <stdio.h>                  // Standard input and output
#include <unistd.h>                 // POSIX standard interface
#include "ohos_init.h"              // Used for initializing services and features
#include "cmsis_os2.h"              // CMSIS-RTOS API V2
#include "iot_gpio.h"               // OpenHarmony HAL: IoT hardware device operation interface - GPIO
#include "../inc/iot_gpio_ex.h"
#include "../inc/oled_ssd1306.h"    // Simplified driver interface file for OLED display

// Character set: 水 流 水 质 金 额 ：
// Character outline: 16 X 16, Song font
// Each character is 32 bytes (16*16/8=32)
// According to the extraction method, the first 16 bytes of each character are the upper half of the Chinese character, and the last 16 bytes are the lower half of the Chinese character
// Using a one-dimensional array
uint8_t fonts2[] = {
    /*-- ID:0, Character:"水", ASCII Encoding:CBAE, Corresponding Character: Width x Height=16x16, Canvas: Width W=16 Height H=16, Total 32 bytes */
    0x00,0x20,0x20,0x20,0x20,0xe0,0x00,0xff,0xc0,0x40,0x20,0x10,0x18,0x00,0x00,0x00,
    0x20,0x10,0x08,0x04,0x03,0x40,0x80,0x7f,0x00,0x01,0x02,0x04,0x08,0x18,0x08,0x00,

    /*-- ID:1, Character:"流", ASCII Encoding:C1F7, Corresponding Character: Width x Height=16x16, Canvas: Width W=16 Height H=16, Total 32 bytes */
    0x10,0x22,0x64,0x0c,0x80,0x44,0x44,0x64,0x55,0x4e,0x44,0x54,0x66,0xc4,0x00,0x00,
    0x04,0x04,0xfe,0x01,0x00,0x80,0x40,0x3f,0x00,0xff,0x00,0x3f,0x40,0x40,0x70,0x00,

    /*-- ID:2, Character:"金", ASCII Encoding:BDF0, Corresponding Character: Width x Height=16x16, Canvas: Width W=16 Height H=16, Total 32 bytes */
    0x80,0x80,0x40,0x20,0x50,0x48,0x44,0xc3,0x44,0x68,0x50,0x20,0x40,0xc0,0x40,0x00,
    0x00,0x40,0x40,0x4a,0x52,0x62,0x42,0x7f,0x42,0x62,0x52,0x4a,0x60,0x40,0x00,0x00,

    /*-- ID:3, Character:"额", ASCII Encoding:B6EE, Corresponding Character: Width x Height=16x16, Canvas: Width W=16 Height H=16, Total 32 bytes */
    0x10,0x8c,0x44,0xb5,0x26,0xa4,0x74,0x0c,0x02,0xf2,0x1a,0xd6,0x12,0xfb,0x12,0x00,
    0x10,0x08,0xfc,0x4a,0x49,0x4a,0x7c,0x08,0x80,0x4f,0x20,0x1f,0x20,0x4f,0xc0,0x00,

    /*-- ID:4, Character:"水", ASCII Encoding:CBAE, Corresponding Character: Width x Height=16x16, Canvas: Width W=16 Height H=16, Total 32 bytes */
    0x00,0x20,0x20,0x20,0x20,0xe0,0x00,0xff,0xc0,0x40,0x20,0x10,0x18,0x00,0x00,0x00,
    0x20,0x10,0x08,0x04,0x03,0x40,0x80,0x7f,0x00,0x01,0x02,0x04,0x08,0x18,0x08,0x00,

    /*-- ID:5, Character:"质", ASCII Encoding:D6CA, Corresponding Character: Width x Height=16x16, Canvas: Width W=16 Height H=16, Total 32 bytes */
    0x00,0x00,0xfe,0x12,0x92,0x92,0x92,0x92,0xfe,0x91,0x91,0x91,0xd1,0x98,0x10,0x00,
    0x40,0x30,0x8f,0x80,0x5f,0x40,0x20,0x10,0x0e,0x20,0x20,0x20,0x5f,0xc0,0x00,0x00,

    /*-- ID:6, Character:"：", ASCII Encoding:A3BA, Corresponding Character: Width x Height=16x16, Canvas: Width W=16 Height H=16, Total 32 bytes */
    0x00,0x00,0x00,0x00,0x80,0xc0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0x00,0x00,0x31,0x7b,0x7b,0x31,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
    };

/**
 * @brief Display Chinese characters on OLED.
 * @param x x-coordinate, in units of 1 pixel
 * @param y y-coordinate, in units of 8 pixels. That is, the page start address
 * @param idx Index of the Chinese character in the font library
 * @author Maker Studio
 * @license Apache 2.0
 */
void OledShowChinese2(uint8_t x, uint8_t y, uint8_t idx)
{
    // Control loop
    uint8_t t;

    // Display the upper half of the Chinese character
    OledSetPosition(x, y);
    for (t = 0; t < 16; t++)
    {
        WriteData(fonts2[32 * idx + t]);
    }

    // Display the lower half of the Chinese character
    OledSetPosition(x, y + 1);
    for (t = 16; t < 32; t++)
    {
        WriteData(fonts2[32 * idx + t]);
    }
}

/**
 * @brief Initialize the OLED display and set the initial display content.
 * This function initializes the SSD1306 OLED display driver chip and fills the screen with black,
 * preparing it for subsequent display operations. It should be called once at system startup.
 * @author Maker Studio
 * @license Apache 2.0
 */
void OledInitTask(void)
{
    // Initialize the SSD1306 display driver chip
    OledInit();

    // Fill the screen with black to ensure the screen starts from a clean state
    OledFillScreen(0x00);
}

/**
 * @brief OLED display fixed text display function.
 * This function is used to display fixed Chinese characters on the OLED screen.
 * It defines the content to be displayed at specific positions on the screen and can be called periodically as an RTOS task or a regular function.
 * @author Maker Studio
 * @license Apache 2.0
 */
void OledTask(void)
{
    // Display "水流:" in the upper left corner (assuming index 0 corresponds to "水", index 1 corresponds to "流", and index 6 corresponds to ":")
    OledShowChinese2(0, 0, 0);  // Display "水"
    OledShowChinese2(18, 0, 1); // Display "流"
    OledShowChinese2(36, 0, 6); // Display ":"

    // Display "金额:" slightly lower (assuming index 2 corresponds to "金", index 3 corresponds to "额", and index 6 corresponds to ":")
    OledShowChinese2(0, 2, 2);  // Display "金"
    OledShowChinese2(18, 2, 3); // Display "额"
    OledShowChinese2(36, 2, 6); // Display ":"

    // Display "水质:" on the next line (assuming index 4 corresponds to "水", index 5 corresponds to "质", and index 6 corresponds to ":")
    OledShowChinese2(0, 4, 4);  // Display "水"
    OledShowChinese2(18, 4, 5); // Display "质"
    OledShowChinese2(36, 4, 6); // Display ":"
}